package org.jeecg.controller;

import lombok.extern.slf4j.Slf4j;

import org.jeecg.domain.User;
import org.jeecg.result.ResultTool;
import org.jeecg.service.UserService;
import org.jeecg.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Slf4j
@RestController
@RequestMapping("/login")
@CrossOrigin(origins="*",maxAge=3600)
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResultTool login(@RequestBody User user){
        try {
            User user1 = userService.loginUser(user);
            String token = JwtUtil.sign(user.getUsername(), user.getPassword());
            HashMap map = new HashMap();
            map.put("user",user1);
            map.put("token",token);
            System.out.println("map = " + map);
            return new ResultTool(map,200);
        }catch (Exception e){
            return new ResultTool<>(400,e.getMessage());
        }
    }

//    @RequestMapping(value = "/logout",method = RequestMethod.GET)
//    public ResultTool logout(@RequestBody User user){
//
//    }


}
